/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.eclipse.create_toolbox_project.ui;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;

import com.genia.toolbox.eclipse.create_toolbox_project.business.CreateToolboxProject;

/**
 * Wizard to create a trampoline project.
 */
public final class NewToolboxProjectWizard extends Wizard implements INewWizard {

	/**
	 * Page contained by this wizard.
	 */
	private NewToolboxProjectMainPage newToolboxProjectMainPage;

	/**
	 * Workbench selection when the wizard was started.
	 */
	private IStructuredSelection selection;

	/**
	 * The workbench instance.
	 */
	private IWorkbench workbench;

	/**
	 * Project to create.
	 */
	private CreateToolboxProject newProject;

	/**
	 * Indicates if the wizard can finish.
	 * 
	 * @return true if the wizard can finish
	 */
	@Override
  public boolean canFinish() {
		return newToolboxProjectMainPage.canFinish();
	}

	/**
	 * Add page to this wizard, we have just one : NewToolboxProjectMainPage.
	 */
	@Override
  public void addPages() {
		newToolboxProjectMainPage = new NewToolboxProjectMainPage(workbench,
				selection);
		addPage(newToolboxProjectMainPage);
	}

	/**
	 * Init method.
	 * 
	 * @param workbench
	 *            to use
	 * @param selection
	 *            to use
	 */
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		this.workbench = workbench;
		this.selection = selection;
	}

	/**
	 * Method called when user finish the wizard. We create project at this
	 * point.
	 * 
	 * @return true if wizard is finished
	 */
	@Override
  public boolean performFinish() {

		try {

			String projectName = newToolboxProjectMainPage.getProjectName()
					.getText();
			String projectPath = newToolboxProjectMainPage.getProjectLocation()
					.getText();
			newProject = new CreateToolboxProject(projectName, projectPath);
			newProject.runProject();

			importProjects();

			MessageDialog.openInformation(workbench.getActiveWorkbenchWindow()
					.getShell(), "success", "project created succesfully");

		} catch (Throwable e) {
			
			MessageDialog.openError(workbench.getActiveWorkbenchWindow()
					.getShell(), "error", e.getMessage());

		}

		return true;
	}

	/**
	 * Imports projects into workspace.
	 * 
	 * @throws CoreException
	 *             if any
	 * @throws FileNotFoundException
	 *             if any
	 */
	private void importProjects() throws CoreException, FileNotFoundException {

		final String[] projectsNameToImport = {
				newProject.getParent().getName()
						+ CreateToolboxProject.SRC_PROJECT_SUFFIX,
				newProject.getParent().getName()
						+ CreateToolboxProject.MODEL_PROJECT_SUFFIX,
				newProject.getParent().getName()
						+ CreateToolboxProject.WAR_PROJECT_SUFFIX };

		final IWorkspace workspace = ResourcesPlugin.getWorkspace();

		for (String projectNameToImport : projectsNameToImport) {

			final IProject project = workspace.getRoot().getProject(
					projectNameToImport);

			final File projectFileToImport = new File(newProject.getParent(),
					projectNameToImport);

			final IProjectDescription desc = workspace
					.loadProjectDescription(new FileInputStream(new File(
							projectFileToImport, ".project")));
			desc.setName(projectNameToImport);
			desc.setLocationURI(projectFileToImport.toURI());

			project.create(desc, null);
			project.open(IResource.BACKGROUND_REFRESH, null);

		}

	}

}
